<?php
namespace App\Services\Admin;

use App\Models\AreaModel;
use App\Repositories\Admin\CompanyRepository;
use App\Repositories\Admin\StaffRepository;
use App\Repositories\Admin\UserImgTmpRepository;
use App\Repositories\Admin\UserRepository;

class UserService
{
    private $service;
    protected $staff;
    protected $com;

    public function __construct()
    {
        $this->service = new UserRepository();
        $this->staff = new StaffRepository();
        $this->com = new CompanyRepository();
    }

    public function getIndex()
    {
        return $this->service->getIndexList();
    }

    public function add($data)
    {
        return $this->service->setAdd($data);
    }

    public function modify($data,$id)
    {
        return $this->service->setModify($data,$id);
    }

    public function setPwd($id,$pwd)
    {
        return $this->service->setPwdByUid($id,$pwd);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    /**
     * 通过手机号码，获取用户信息
     */
    public function getOneByMobile($mobile)
    {
        return $this->service->getShowOneByMobile($mobile);
    }

    /**
     * 通过手机号码，重置密码
     */
    public function setPwdByMobile($mobile,$pwdHash)
    {
        return $this->service->setPwd($mobile,$pwdHash);
    }

    /**
     * 通过UID设置type
     */
    public function setType($uid,$data)
    {
        return $this->service->setTypeByUid($uid,$data);
    }

    /**
     * 设置用户当前坐标
     */
    public function setPoint($uid,$point)
    {
        return $this->service->setTypeByUid($uid,$point);
    }

    /**
     * 用户类型名称
     */
    public function getTypeName($type)
    {
        return $this->service->getTypeNameByType($type);
    }

    const Type_null=0;
    const Type_yuangong=1;
    const Type_gongsi=2;
    const Type_gongchang=3;
    const Type_gongsi_gongchang=4;

    const Cate_gongchang=1;
    const Cate_gongsi=2;
    const Cate_gongsi_gongchang=3;


    /**
     * 通过主体类型，用户类型
     */
    public function getUserType($uid,$type)
    {
        //个人
        if ($type==1) {
            $staffModel = $this->staff->getShowOne($uid);
            return $staffModel ? $staffModel->cate : 1; //默认工厂员工
        }
        //企业
        $comModel = $this->com->getShowOneByUid($uid);
        if ($comModel) { return $comModel->cate; }
        //没有详细信息
        $cate = 0;
        if ($type==2) {
            $cate = 2;
        } else if ($type==3) {
            $cate = 1;
        } else if ($type==4) {
            $cate = 4;
        }
        return $cate;
    }

    /**
     * 临时图片设置
     */
    public function setImgTmp($data)
    {
        $model = new UserImgTmpRepository();
        return $model->setAdd($data);
    }

    /**
     * 获取省
     */
    public function getProvices()
    {
        return AreaModel::where('pid',100000)->get();
    }

    /**
     * 获取市、区
     */
    public function getCityByPid($pid)
    {
        return AreaModel::where('pid',$pid)->get();
    }

    /**
     * 账户余额获取
     */
    public function getUserFinance($uid)
    {
        return $this->service->getUserFinance($uid);
    }

    /**
     * 账户余额充值
     */
    public function setFinanceIncome($uid,$money)
    {
        return $this->service->setFinanceIncome($uid,$money);
    }

    /**
     * 账户余额提现
     */
    public function setFinanceOut($uid,$money)
    {
        return $this->service->setFinanceOut($uid,$money);
    }

    /**
     * 账户交易，减少付款人余额
     */
    public function setPayerFinance($uid,$money)
    {
        return $this->service->setPayerFinance($uid,$money);
    }

    /**
     * 账户交易，增加收款人余额、总收益
     */
    public function setReceiver($uid,$money)
    {
        return $this->service->setReceiver($uid,$money);
    }

    /**
     * 用户须知
     */
    public function setUserNotice($uid)
    {
        return $this->service->setUserNotice($uid);
    }

    /**
     * 平台交易密码设置
     */
    public function setTradePwd($uid,$pwd)
    {
        return $this->service->setTradePwd($uid,$pwd);
    }
}